<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>系统常态化运行评分系统</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css" />
    <style>
        .expand.pear-btn:hover {
            color: currentColor;
            background: none;
        }

        .expand.pear-btn {
            border: 1px solid rgba(255, 255, 255, 0)
        }
        .layui-table-box button {
            font-size: 12.5px !important;
            float: left;
            margin-right: 5px; /* 根据需要调整间距 */
        }
    </style>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="userForm" class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">检索</label>
                    <div class="layui-input-inline">
                        <input type="text" name="search" placeholder="请输入用户姓名或者账号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-inline">
                        <select  name="roleId" class="city-selector">
                            <option value="">请选择角色</option>
                            <option value="3">矿区</option>
                            <option value="4">矿井</option>
                        </select>
                    </div>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>


<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addUser">
        <i class="layui-icon layui-icon-addition"></i>
        新增用户
    </button>
</script>


<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="resetLoginPasswordItem" title="重置密码"><i class="layui-icon layui-icon-refresh"></i>重置</button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="modify" title="修改用户信息"><i class="layui-icon layui-icon-edit"></i>修改</button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del" title="删除用户信息及账号"><i class="layui-icon layui-icon-delete"></i>删除</button>
</script>

<script type="text/html" id="loginState">
    <input type="checkbox" name="loginState" value="{{JSON.stringify(d)}}" lay-skin="switch" lay-text="是|否" lay-filter="loginState"
           {{ d.loginState!=null && d.loginState===1? "checked" : "" }} />
</script>


<div style="display: none;">
    <div class="layer-top">
        <div class="layui-card">
            <div class="layui-card-header" id="divHeader">新增用户</div>
        </div>
        <div class="layui-card" style="margin-top: 10px;">
            <form class="layui-form" action="" onsubmit="return false" lay-filter="lay-info">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" required  lay-verify="required" placeholder="请输入用户姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="loginName" required  lay-verify="required" placeholder="请输入登录账号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <select id="roleId"  name="roleId" lay-filter="roleId">
                            <!-- <option value="1">股份</option>
                            <option value="2">专家</option> -->
                            <option value="3">矿区</option>
                            <option value="4">矿井</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="divBelong" style="display: none;">
                    <label class="layui-form-label">矿区/矿井</label>
                    <div class="layui-input-block">
                        <select id="belongId" name="belongId" lay-search>

                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">账号状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="loginState" value="1" title="可用"  checked>
                        <input type="radio" name="loginState" value="2" title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="pear-btn pear-btn-md pear-btn-primary" id="btn" lay-submit lay-filter="lay-user-add">新增用户</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script src="../../scripts/utils/request.js"></script>
<script src="../../scripts/utils/auth.js"></script>
<script>
    var $,table,form,drawer,popup,notice
    layui.use(['table', 'form', 'jquery', 'drawer', 'popup','notice'], function() {
        table = layui.table;
        form = layui.form;
        $ = layui.jquery;
        drawer = layui.drawer;
        popup = layui.popup;
        notice = layui.notice;

        table.render({
            id:"user-table",
            elem: '#user-table',
            url: baseURL+'/api/user/list',
            method:'post',
            contentType: 'application/json',
            page: true,
            loading:true,
            headers: {
                token: localStorage.getItem("token")
            },
            request: {
                pageName: 'pageIndex', // page
                limitName: 'pageSize', // limit
            },
            where:{
                search:$("#search").val()
            },
            cols: [[
                {field: 'userName', minWidth: 200, title: '用户名'},
                {field: 'loginName', minWidth: 200, title: '登录账号'},
                {field: 'mineName', minWidth: 200, title: '所属矿区/矿井'},
                {field: 'isManager', minWidth: 150, title: '是否管理员',templet:function(d){
                    return d.isManager?'<span class="layui-font-green">是</span>':'<span>否</span>';
                }
                },
                {field: 'loginState', title: '账号状态',templet: '#loginState'},
                {field: 'roleIdName', title: '角色'},
                {title: '操作',templet: '#user-bar', width: 300, align: 'center'}
            ]],
            skin: 'grid',
            toolbar: '#user-toolbar',
            defaultToolbar: [],
            done: function(res, curr, count) {
                validToken(res);
            }
        });

        
        var mineData=[];
        var mineAreaData=[];
        postAjaxNoSuccessTip('/api/mine/getAllMineListBySelect',null,function(response){
            for (let i = 0; i < response.data.length; i++) {
                mineAreaData.push({"id":response.data[i].id,"name":response.data[i].name})
                for (let j = 0; j < response.data[i].children.length; j++) {
                    mineData.push({"id":response.data[i].children[j].id,"name":response.data[i].children[j].name})
                } 
            } 
        },null);

        function handleChange(){
            $("#belongId").empty();
            let selIndex=$("#roleId").get(0).selectedIndex;
            if(selIndex===0){
                $("#divBelong").css("display","block");
                //获取矿区数据
                for (let i = 0; i < mineAreaData.length; i++) {
                    $("#belongId").append(new Option(mineAreaData[i].name,mineAreaData[i].id));
                }
            }else if(selIndex===1){
                $("#divBelong").css("display","block");
                //获取矿井数据
                 for (let i = 0; i < mineData.length; i++) {
                    $("#belongId").append(new Option(mineData[i].name,mineData[i].id));
                }
            }
            form.render("select");
        }
        
        form.on('select(roleId)', function(data){
            handleChange();
        }); 

        /**
         * 每行的按钮
         */
        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'resetLoginPasswordItem') {
                resetPasswordItem(obj);
            } else if (obj.event === 'del') {
                delUser(obj);
            }else if(obj.event==='modify'){
                form.val('lay-info',{
                    "id":obj.data.id,
                    "userName":obj.data.userName,
                    "loginName":obj.data.loginName,
                    "roleId":obj.data.roleId,
                    "belongId":obj.data.belongId,
                    "loginState":obj.data.loginState,
                });
                handleChange();
                $("#belongId").val(obj.data.belongId)
                form.render();
                
                $('#divHeader').html('修改用户');
                $('#btn').text('修改用户');
                drawer.open({
                    direction: "right",
                    dom: ".layer-top",
                    distance: "40%"
                });
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'addUser') {
                window.addUser();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                },
            })
            return false;
        });
        /**
         * 用户状态编辑
         */
        form.on('switch(loginState)', function(obj) {
            let val=$(this).attr('value');
            let data= JSON.parse(val);
            let reqData={};
            reqData.id=data.id;
            if(obj.elem.checked){
                reqData.loginState=1;
            }else{
                reqData.loginState=2;
            }
            postAjax('/api/user/modifyState',reqData,function (response){
                table.reload('user-table');
            },null);
        });

        /**
         * 新增用户
         */
        form.on('submit(lay-user-add)',function (data){
            let reqData=data.field;
            if(reqData.roleId==1||reqData.roleId==2){
                reqData.belongId=null;
            }else{
                if(!reqData.belongId||reqData.belongId==''){
                    notice.error('请选择所属矿区或者矿井');
                    return false;
                }
            }
            if(reqData.id==''){
                postAjaxNoSuccessTip('/api/user/add',data.field,function (response){
                    notice.success(response.data);
                    drawer.closeAll();
                    table.reload('user-table');
                },null);
            }else{
                postAjax('/api/user/modify',data.field,function (response){
                    drawer.closeAll();
                    table.reload('user-table');
                },null);
            }

        });


        window.refresh = function(param) {
            table.reload('user-table');
        };

        /**
         * 新增用户
         */
        window.addUser=function (){
            form.val('lay-info',{
                "id":'',
                "userName":'',
                "loginName":'',
                "roleId":'1',
                "belongId":'',
                "loginState":'1',
            });
            handleChange();
            $('#divHeader').html('新增用户');
            $('#btn').text('新增用户');
            form.render();
            drawer.open({
                direction: "right",
                dom: ".layer-top",
                distance: "40%"
            });
        };


        /**
         * 重置密码
         * @param obj
         */
        function resetPasswordItem(obj){
            let idList=[];
            idList.push(obj.data.id)
            let reqData={idList:idList};
            layer.confirm('确定重置用户登录密码？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                postAjaxNoSuccessTip('/api/user/resetLoginPassword',reqData,function (response){
                    notice.success(response.data);
                    window.refresh();
                },null);
            });
        }

        /**
         * 删除用户
         * @param obj
         */
        function delUser(obj){
            let reqData={id:obj.data.id};
            layer.confirm('确定删除用户？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                postAjax('/api/user/delete',reqData,function (response){
                    window.refresh();
                },null);
            });
        }
    })
</script>
</body>

</html>
